﻿<template id="@(ViewBag.PageTemplate)">
    <div class="layout-page-content">
        <div class="layout-page-content-box">
            <h2>数据实体业务生成器</h2>
            <el-divider class="layout-page-form-top-border"></el-divider>
            <el-form ref="formRef" :rules="rules" :disabled="disabled" :model="record" :hide-required-asterisk="disabled" label-width="auto" label-suffix=" :">
                <el-form-item label="数据库表名" prop="tableName">
                    <el-input v-model="record.tableName" placeholder="请输入数据库表名，按回车后生成相应 Entity 代码" clearable v-on:keyup.enter.native="getEntityName" v-on:clear="reset"></el-input>
                </el-form-item>
                <el-form-item label="分区名称" prop="area">
                    <el-input v-model="record.area" placeholder="分区名称可能需要手动调整，请根据模块名称自行确定"></el-input>
                </el-form-item>
                <el-form-item label="数据库 ConfigId" prop="tenant">
                    <el-input v-model="record.tenant"></el-input>
                </el-form-item>
                <el-form-item label="实体名称" prop="entity">
                    <el-input v-model="record.entity"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="generate">创建实体</el-button>
                    <el-button type="warning" v-on:click="generateProps">生成 ServiceProps</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script type="text/javascript">
    var @(ViewBag.PageName) = {
        template: "#@(ViewBag.PageTemplate)", data() {
            return {
                path: "@(ViewBag.PagePath)",
                rules: {
                    tableName: [{ required: true, message: "请填写数据库表名" }],
                    area: [{ required: true, message: "请填写分区名称" }],
                    tenant: [{ required: true, message: "请填写数据库 ConfigId" }],
                    entity: [{ required: true, message: "请填写实体名称" }]
                },
                record: {},
                disabled: false
            }
        },
        methods: {
            reset(action){
                if (action == "clear"){
                    this.record.tableName = null;
                }
                this.record.area = null;
                this.record.tenant = "SingleDefault";
                this.record.entity = null;
            },
            getEntityName(){
                var that = this;
                var tableName = that.record.tableName;
                if (tableName) {
                    that.reset();
                    app.func.getData(`${that.path}/getentityname`, { tableName }, ".layout-page-component", result => {
                        that.record.area = result.data.area;
                        that.record.entity = result.data.entity;
                    });
                }
                else{ that.reset("clear"); }
            },
            generate(){
                var that = this;
                that.$refs.formRef.validate(valid => {
                    if (valid) {
                        app.func.post(`${that.path}/dogenerateentity`, that.record, ".layout-page-component", result => {
                            app.func.alter(result.message);
                        });
                    }
                });
            },
            generateProps(){
                var that = this;
                if(that.record.area){
                    app.func.longAjaxDo(`${that.path}/dogenerateservice`, { name: `Single.${that.record.area}` }, "正在生成项目，预计需要【30秒】请稍后...");
                }
                else{ app.func.alter("生成 ServiceProps 需要填写分区名称"); }
            }
        },
        mounted() { this.reset("clear"); }
    };
</script>